package org.flhy.webapp.utils;

import org.pentaho.di.core.logging.LogLevel;
import org.pentaho.di.job.JobHopMeta;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.job.entries.abort.JobEntryAbort;
import org.pentaho.di.job.entries.special.JobEntrySpecial;
import org.pentaho.di.job.entries.success.JobEntrySuccess;
import org.pentaho.di.job.entries.writetolog.JobEntryWriteToLog;
import org.pentaho.di.job.entry.JobEntryCopy;

/**
 * Created by admin on 2019/3/3.
 */
public class JobUtil {
    /**
     * This method generates a job definition from scratch.
     * <p>
     * It demonstrates the following:
     * <p>
     * - Creating a new job
     * - Creating and connecting job entries
     *
     * @return the generated job definition
     */
    public static JobMeta generateJob() {

        try {
            System.out.println("Generating a job definition");


            // create empty transformation definition
            JobMeta jobMeta = new JobMeta();
            jobMeta.setName("Generated Demo Job");


            // ------------------------------------------------------------------------------------
            // Create start entry and put it into the job
            // ------------------------------------------------------------------------------------
            System.out.println("- Adding Start Entry");

            // Create and configure start entry
            JobEntrySpecial start = new JobEntrySpecial();
            start.setName("START");
            start.setStart(true);

// wrap into JobEntryCopy object, which holds generic job entry information
            JobEntryCopy startEntry = new JobEntryCopy(start);

// place it on Spoon canvas properly
            startEntry.setDrawn(true);
            startEntry.setLocation(100, 100);

            jobMeta.addJobEntry(startEntry);

// ------------------------------------------------------------------------------------
// Create "write to log" entry and put it into the job
// ------------------------------------------------------------------------------------
            System.out.println("- Adding Write To Log Entry");

// Create and configure entry
            JobEntryWriteToLog writeToLog = new JobEntryWriteToLog();
            writeToLog.setName("Output PDI Stats");
            writeToLog.setLogLevel(LogLevel.MINIMAL);
            writeToLog.setLogSubject("Logging PDI Build Information:");
            writeToLog.setLogMessage("Version: ${Internal.Kettle.Version}\n" +
                    "Build Date: ${Internal.Kettle.Build.Date}");

// wrap into JobEntryCopy object, which holds generic job entry information
            JobEntryCopy writeToLogEntry = new JobEntryCopy(writeToLog);

// place it on Spoon canvas properly
            writeToLogEntry.setDrawn(true);
            writeToLogEntry.setLocation(200, 100);

            jobMeta.addJobEntry(writeToLogEntry);

// connect start entry to logging entry using simple hop
            jobMeta.addJobHop(new JobHopMeta(startEntry, writeToLogEntry));

// ------------------------------------------------------------------------------------
// Create "success" entry and put it into the job
// ------------------------------------------------------------------------------------
            System.out.println("- Adding Success Entry");

// crate and configure entry
            JobEntrySuccess success = new JobEntrySuccess();
            success.setName("Success");

// wrap into JobEntryCopy object, which holds generic job entry information
            JobEntryCopy successEntry = new JobEntryCopy(success);

// place it on Spoon canvas properly
            successEntry.setDrawn(true);
            successEntry.setLocation(400, 100);

            jobMeta.addJobEntry(successEntry);

// connect logging entry to success entry on TRUE evaluation
            JobHopMeta greenHop = new JobHopMeta(writeToLogEntry, successEntry);
            greenHop.setEvaluation(true);
            jobMeta.addJobHop(greenHop);

// ------------------------------------------------------------------------------------
// Create "abort" entry and put it into the job
// ------------------------------------------------------------------------------------
            System.out.println("- Adding Abort Entry");

// crate and configure entry
            JobEntryAbort abort = new JobEntryAbort();
            abort.setName("Abort Job");

// wrap into JobEntryCopy object, which holds generic job entry information
            JobEntryCopy abortEntry = new JobEntryCopy(abort);

// place it on Spoon canvas properly
            abortEntry.setDrawn(true);
            abortEntry.setLocation(400, 300);

            jobMeta.addJobEntry(abortEntry);

// connect logging entry to abort entry on TRUE evaluation
            JobHopMeta redHop = new JobHopMeta(writeToLogEntry, abortEntry);
            redHop.setEvaluation(true);
            jobMeta.addJobHop(redHop);


            return jobMeta;


        } catch (Exception e) {


// something went wrong, just log and return
            e.printStackTrace();
            return null;
        }


    }
}
